<?php 

require_once ($DOCUMENT_ROOT . '/includes/functions.php');

session_start ();

if (valid_user ())
{
	// new css include
	$header = '<link type="text/css" rel="StyleSheet" href="css/revised.css" />';
	do_header ('Read Reviews', $header);
	do_menu ();
	
	db_connect ();
	
	if (is_admin ())
	{
		$admin = 1;
	}
	else
	{
		$admin = 0;
	}
		
	// grab the right teacher from the db
	$query = "SELECT * FROM ilist WHERE id='$id'";
	$result = mysql_query($query) 
		or report_error ($PHP_SELF, mysql_error (), "select ilist failure", $_SESSION['valid_user']);

	// parse up the array into short variables
	$line = mysql_fetch_array($result);
	$fname = $line['fName'];
	$lname = $line['lName'];
	$rating = $line['rating'];
	$ratingTotal = $line['ratingTotal'];
		
	echo "<h1>Reviews for: $fname $lname</h1>";
	if ($ratingTotal == 0)
	{
		echo "<h2>Average rating: no rating yet</h2>";
	}
	else
	{
		$avg = $rating / $ratingTotal;
		// round to two decimal places
		$formatted = sprintf("%01.2f", $avg);
		echo "<h2>Average rating: $formatted</h2>";
		echo "<h2>based on $ratingTotal reviews</h2>";
	}
	echo "<h3><a href='write.php?id=$id'>Write a Review for $fname $lname</a></h3>\n";

	// display the reviews for the teacher
	$query = "SELECT * FROM reviews WHERE teacherID='$id'";
	$result = mysql_query($query) 
		or report_error ($PHP_SELF, mysql_error (), "select reviews failure", $_SESSION['valid_user']);
	
	while ($line = mysql_fetch_array($result))
	{
		echo "<table class='review'>";
		// resolve id of review for vote
		$voteid = $line['id'];
// change to 1 when out of beta
		if ($line[approved] == 0)
		{
			// handle the vote count output
			$query = "SELECT * FROM vote WHERE id='$voteid'";
			$vresult = mysql_query ($query)
				or report_error ($PHP_SELF, mysql_error (), "select vote failure", $_SESSION['valid_user']);
			$vote = 0;
			$voteTotal = 0;
			while ($vline = mysql_fetch_array($vresult))
			{
				$vote += $vline['positive'];
				$voteTotal++;
			}
			
			echo "<tr><td colspan='2'>";
			echo "<p>" . $vote . " of " . $voteTotal . " users found this review helpful</p>";
			echo "</td></tr>";
			
			// put in the remove link for the admin
			if ($admin == 1)
			{
				$id = $line['id'];
				if ($voteTotal == 0)
				{
					$ratio = 0;
				}
				else
				{
					$ratio = $vote / $voteTotal;
				}
				// find out who wrote it
				if ($line[authorID] == 0)
				{
					$aname = "default";
				}
				else
				{
					$query = "SELECT name FROM users WHERE id='$line[authorID]'";
					$aresult = mysql_query ($query);
					$arow = mysql_fetch_row ($aresult);
					$aname = $arow[0];
				}
				echo "<tr><td colspan='2' class='admin'>";
				echo "<p>$aname ratio: $ratio based on $voteTotal votes <a href='/admin/removereview.php?id=$id'>remove review</a></p>";
				echo "</td></tr>";
			}
			
			// strip slashes off of comment and format it
			$comment = stripslashes ($line['comment']);
			$comment = nl2br ($comment);
			
			// print a table with each review
			echo "<tr><td class='info'>
				  <ul><li>Rating: $line[rating]</li>
				  <li>Class taken: $line[class]</li>
				  <li>Term: $line[term]</li></ul></td>
				  <td><p class='comment'>$comment</p></td></tr>";
			
			// handle voting for a review
			echo "<tr><td colspan='2'>";
			echo "<p>Was this review helpful to you?
					<a href='vote.php?id=$voteid&vote=1'>yes</a>  - 
					<a href='vote.php?id=$voteid&vote=0'>no</a></p>";
			echo "</td></tr>";
			
		}
		echo "</table>";
	}
	
	do_footer ();
}
?>
